package com.aretias.jkholdings.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aretias.jkholdings.model.Category;
import com.aretias.jkholdings.model.Product;
import com.aretias.jkholdings.model.ProductParam;
import com.aretias.jkholdings.service.ProductService;

@Controller
public class ProductController extends BaseController{
	
	@Autowired
	private ProductService productService;  
	
	@RequestMapping("/products")
	public ModelAndView showProducts(ProductParam param, HttpSession session) {
		
		ModelAndView mav = new ModelAndView("products");
		
		// 왼쪽에 CATEGORY 를 가져온다.
		Category category = new Category();
		category.setParentCategoryId(0);
		List<Category> categoryList = categoryService.getChild(category);
		
		// 상품리스트를 가져온다.
		List<Product> productList = productService.getProductsList(param);
		
		// 페이징관련된 정보를 만든다.
		param.setLimitParam();
		setPageInfo(mav, ProductParam.PAGE_SIZE, ProductParam.PAGE_BLOCK_SIZE, productService.getProductsListCount(param), param.getPageNumber());
		
		// 해당 카테고리 FULLPATH를 가져온다.
		category.setCategoryId(param.getProductCategoryId());
		category = categoryService.getCategoryFullName(category);
		
		mav.addObject("categoryList", categoryList);
		mav.addObject("productList", productList);
		mav.addObject("category", category);
		mav.addObject("session",getUserForSession(session));
		
		return mav;
	}
	
	@RequestMapping("/products/view/{productId}")
	public ModelAndView showProductDetail(@PathVariable int productId, HttpSession session) {
		
		Category category = new Category();
		category.setParentCategoryId(0);
		
		ProductParam param = new ProductParam();
		param.setProductId(productId);
		
		Product product = productService.getProduct(param);
		category.setCategoryId(product.getProductCategoryId());
		List<Category> categoryList = categoryService.getChild(category);
		category = categoryService.getCategoryFullName(category);
		
		ModelAndView mav = new ModelAndView("productDetail");
		mav.addObject("product", product);
		mav.addObject("categoryList", categoryList);
		mav.addObject("category", category);
		mav.addObject("session",getUserForSession(session));
		return mav;
	}
	
	@RequestMapping("/products/form")
	public ModelAndView showProductForm(ProductParam param, HttpSession session) {
		
		Category category = new Category();
		category.setParentCategoryId(0);
		
		List<Category> categoryList = categoryService.getChild(category);
		List<Category> categorySelectBoxList = categoryService.gettAllCategorys();  
		Product product = productService.getProduct(param);
		
		ModelAndView mav = new ModelAndView("productForm");
		mav.addObject("categoryList", categoryList);
		mav.addObject("categorySelectBoxList", categorySelectBoxList);
		mav.addObject("product", product);
		
		return mav;
	}
	
	@RequestMapping("/products/modifyForm")
	public ModelAndView showModifyProductForm(ProductParam param, HttpSession session) {
		
		Category category = new Category();
		category.setParentCategoryId(0);
		
		List<Category> categoryList = categoryService.getChild(category);
		List<Category> categorySelectBoxList = categoryService.gettAllCategorys();  
		Product product = productService.getProduct(param);
		
		ModelAndView mav = new ModelAndView("productModifyForm");
		mav.addObject("categoryList", categoryList);
		mav.addObject("categorySelectBoxList", categorySelectBoxList);
		mav.addObject("product", product);
		
		return mav;
	}
	
	@RequestMapping(value = "/products/form/add" , method = RequestMethod.POST)
	public ModelAndView addProduct(ProductParam param, HttpServletRequest request) throws IOException {
		
		//String fileUpLoadPath = request.getRealPath(request.getContextPath()) + "/UpLoad/";
		String fileUpLoadPath = "/home/hosting_users/jk6504/www/UpLoad/";
		param.setProductFileInit();
		
		File productImageUpLoadFile = new File(fileUpLoadPath + param.getProductImageName());
		FileOutputStream productImageOutStream = new FileOutputStream(productImageUpLoadFile);
		productImageOutStream.write(param.getProductImage().getBytes(),0,(int)param.getProductImage().getSize());
		productImageOutStream.flush();
		productImageOutStream.close();
		
		File productThumbnailUpLoadFile = new File(fileUpLoadPath + param.getProductThumbnailName());
		FileOutputStream productThumbnailOutStream = new FileOutputStream(productThumbnailUpLoadFile);
		productThumbnailOutStream.write(param.getProductThumbnail().getBytes(),0,(int)param.getProductThumbnail().getSize());
		productThumbnailOutStream.flush();
		productThumbnailOutStream.close();

		int productId = productService.addProduct(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/products/view/"+Integer.toString(productId)+".do");
		return mav;
	}
	
	@RequestMapping(value = "/products/form/remove" , method = RequestMethod.POST)
	public ModelAndView removeProduct(ProductParam param, HttpSession session) {
		productService.removeProduct(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/products.do");
		return mav;
	}
	
	@RequestMapping(value = "/products/form/update", method = RequestMethod.POST)
	public ModelAndView updateProduct(ProductParam param, HttpServletRequest request)  throws IOException {
		
		//String fileUpLoadPath = request.getRealPath(request.getContextPath()) + "/UpLoad/";
		String fileUpLoadPath = "/home/hosting_users/jk6504/www/UpLoad/";
		param.setProductFileInit();
		
		if(!"".equals(param.getProductImageName())){
			File productImageUpLoadFile = new File(fileUpLoadPath + param.getProductImageName());
			FileOutputStream productImageOutStream = new FileOutputStream(productImageUpLoadFile);
			productImageOutStream.write(param.getProductImage().getBytes(),0,(int)param.getProductImage().getSize());
			productImageOutStream.flush();
			productImageOutStream.close();
		}else{
			param.setProductImageName(null);
		}
		if(!"".equals(param.getProductThumbnailName())){
			File productThumbnailUpLoadFile = new File(fileUpLoadPath + param.getProductThumbnailName());
			FileOutputStream productThumbnailOutStream = new FileOutputStream(productThumbnailUpLoadFile);
			productThumbnailOutStream.write(param.getProductThumbnail().getBytes(),0,(int)param.getProductThumbnail().getSize());
			productThumbnailOutStream.flush();
			productThumbnailOutStream.close();
		}else{
			param.setProductThumbnailName(null);
		}
		
		productService.modifyProduct(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/products/view/"+Integer.toString(param.getProductId())+".do");
		return mav;
	}
}